package cn.mesmile.xxljob.service.jobhadler;

import cn.hutool.core.util.StrUtil;
import cn.mesmile.xxljob.common.R;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zb
 * @date 2021/7/19 9:50
 * @Description
 */
@RequestMapping("/api/v1/rest")
@RestController
public class GlueJavaJobHandler  {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/login")
    public ReturnT<String> execute(){
        String url = "http://81.69.43.78:8090/xxl-job-admin/login";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        //提交参数设置
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.put("userName", Collections.singletonList("admin"));
        params.put("password", Collections.singletonList("123456"));

        HttpEntity<MultiValueMap<String, String>> request =
                new HttpEntity<MultiValueMap<String, String>>(params, httpHeaders);
        ResponseEntity<Object> objectResponseEntity = restTemplate.postForEntity(url, request, Object.class);

        Object body = objectResponseEntity.getBody();

        HttpHeaders headers = objectResponseEntity.getHeaders();
        // XXL_JOB_LOGIN_IDENTITY=7b226964223a312c22757365726e616d65223a2261646d696e222c2270617373776f7264223a226531306164633339343962613539616262653536653035376632306638383365222c22726f6c65223a312c227065726d697373696f6e223a6e756c6c7d; Path=/; HttpOnly
        List<String> strings = headers.get("Set-Cookie");
        if (strings != null && !strings.isEmpty()){
            String s = strings.get(0);
            String[] split = StrUtil.split(s, ";");
            System.out.println(split[0]);
        }

        System.out.println("-----------------------");
        return ReturnT.SUCCESS;
    }

    @GetMapping("/list")
    public R getList(){
        String url = "http://81.69.43.78:8090/xxl-job-admin/jobinfo/pageList";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        List<String> objects = new ArrayList<>();
        objects.add("XXL_JOB_LOGIN_IDENTITY=7b226964223a312c22757365726e616d65223a2261646d696e222c2270617373776f7264223a226531306164633339343962613539616262653536653035376632306638383365222c22726f6c65223a312c227065726d697373696f6e223a6e756c6c7d; Path=/; HttpOnly");
        httpHeaders.put(HttpHeaders.COOKIE, objects);

        /**
         * jobGroup: 1
         * triggerStatus: -1
         * jobDesc:
         * executorHandler:
         * author:
         * start: 0
         * length: 10
         */
        //提交参数设置
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.put("jobGroup", Collections.singletonList(1));
        params.put("triggerStatus", Collections.singletonList(-1));
        params.put("jobDesc", Collections.singletonList(""));
        params.put("executorHandler",  Collections.singletonList(""));
        params.put("author",  Collections.singletonList(""));
        params.put("start", Collections.singletonList(0));
        params.put("length", Collections.singletonList(10));

        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(params, httpHeaders);
        ResponseEntity<Object> objectResponseEntity = restTemplate.postForEntity(url, request, Object.class);
        Object body = objectResponseEntity.getBody();

        HttpHeaders headers = objectResponseEntity.getHeaders();

        return R.data(body);
    }
}
